package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.KeyGen;

import common.Constant;

import model.Commented;

public class CommentedDao extends RpDao {

	
			public List<Commented> GetMyCommented(String uid,int page){
				List<Commented> mycomments=new ArrayList<Commented>();
				Connection conn=this.getConn();
				PreparedStatement ps=null;
				ResultSet rs=null;
				try {
					ps = conn.prepareStatement(SqlString.selectCommentByUid);
					int i=1;
					ps.setString(i++, uid);
					ps.setInt(i++, page*Constant.PageSize);
					ps.setInt(i++, Constant.PageSize);
					rs=ps.executeQuery();
					while (rs.next()){
						Commented ctd=new Commented();
						ctd.setComment(rs.getString("content"));
						ctd.setCtime(rs.getString("ctime"));
						ctd.setItem(KeyGen.convertId2Code(rs.getInt("itemid")));
						ctd.setItemname(rs.getString("name"));
						ctd.setItemimg(rs.getString("img"));
						ctd.setType(rs.getInt("type"));//评论
						if (ctd.getType()!=0){//投票
							ResultSet rs2=conn.createStatement().executeQuery("select content from comment where id="+ctd.getType());
							while (rs2.next()){
								ctd.setComment(rs2.getString("content"));
							}
							rs2.close();
						}
						mycomments.add(ctd);
					}				
//					//加载评论过的
//					ps = this.rp_conn.prepareStatement(SqlString.selectCommentByUid);
//					i=0;
//					ps.setString(i++, uid);
//					ps.setInt(i++, page);
//					ps.setInt(i++, Constant.PageSize);
//					rs=ps.executeQuery();
//					while (rs.next()){
//						Commented ctd=new Commented();
//						ctd.setComment(rs.getString("content"));
//						ctd.setCtime(rs.getString("ctime"));
//						ctd.setItem(rs.getString("itemid"));
//						ctd.setItemname(rs.getString("name"));
//						ctd.setItemimg(rs.getString("img"));
//						ctd.setType(0);//评论
//						mycomments.add(ctd);
//					}
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
				}finally{
					this.closeDao(conn, ps, rs);
				}
				
				return mycomments;
			}
			
			public int getCommentedCount(String uid){
				int n=0;
				Connection conn=this.getConn();
				PreparedStatement ps = null;
				ResultSet rs = null;
				try {
					ps=conn.prepareStatement(SqlString.selectCommentedCount);
					ps.setString(1, uid);
					rs =ps.executeQuery();
					while (rs.next()){
						n=rs.getInt("commentedcount");
					}
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					this.closeDao(conn, ps, rs);
				}
				
				return n;
			}
			
}
